/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.portlet.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.RowMapper;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.portlet.NoSuchroleException;

import com.portlet.model.impl.roleImpl;
import com.portlet.model.impl.roleModelImpl;
import com.portlet.model.role;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the role service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Thi
 * @see rolePersistence
 * @see roleUtil
 * @generated
 */
public class rolePersistenceImpl extends BasePersistenceImpl<role>
	implements rolePersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link roleUtil} to access the role persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = roleImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(roleModelImpl.ENTITY_CACHE_ENABLED,
			roleModelImpl.FINDER_CACHE_ENABLED, roleImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(roleModelImpl.ENTITY_CACHE_ENABLED,
			roleModelImpl.FINDER_CACHE_ENABLED, roleImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(roleModelImpl.ENTITY_CACHE_ENABLED,
			roleModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);

	/**
	 * Caches the role in the entity cache if it is enabled.
	 *
	 * @param role the role
	 */
	public void cacheResult(role role) {
		EntityCacheUtil.putResult(roleModelImpl.ENTITY_CACHE_ENABLED,
			roleImpl.class, role.getPrimaryKey(), role);

		role.resetOriginalValues();
	}

	/**
	 * Caches the roles in the entity cache if it is enabled.
	 *
	 * @param roles the roles
	 */
	public void cacheResult(List<role> roles) {
		for (role role : roles) {
			if (EntityCacheUtil.getResult(roleModelImpl.ENTITY_CACHE_ENABLED,
						roleImpl.class, role.getPrimaryKey()) == null) {
				cacheResult(role);
			}
			else {
				role.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all roles.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(roleImpl.class.getName());
		}

		EntityCacheUtil.clearCache(roleImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the role.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(role role) {
		EntityCacheUtil.removeResult(roleModelImpl.ENTITY_CACHE_ENABLED,
			roleImpl.class, role.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@Override
	public void clearCache(List<role> roles) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (role role : roles) {
			EntityCacheUtil.removeResult(roleModelImpl.ENTITY_CACHE_ENABLED,
				roleImpl.class, role.getPrimaryKey());
		}
	}

	/**
	 * Creates a new role with the primary key. Does not add the role to the database.
	 *
	 * @param maRole the primary key for the new role
	 * @return the new role
	 */
	public role create(String maRole) {
		role role = new roleImpl();

		role.setNew(true);
		role.setPrimaryKey(maRole);

		return role;
	}

	/**
	 * Removes the role with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param maRole the primary key of the role
	 * @return the role that was removed
	 * @throws com.portlet.NoSuchroleException if a role with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public role remove(String maRole)
		throws NoSuchroleException, SystemException {
		return remove((Serializable)maRole);
	}

	/**
	 * Removes the role with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the role
	 * @return the role that was removed
	 * @throws com.portlet.NoSuchroleException if a role with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public role remove(Serializable primaryKey)
		throws NoSuchroleException, SystemException {
		Session session = null;

		try {
			session = openSession();

			role role = (role)session.get(roleImpl.class, primaryKey);

			if (role == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchroleException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(role);
		}
		catch (NoSuchroleException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected role removeImpl(role role) throws SystemException {
		role = toUnwrappedModel(role);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, role);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		clearCache(role);

		return role;
	}

	@Override
	public role updateImpl(com.portlet.model.role role, boolean merge)
		throws SystemException {
		role = toUnwrappedModel(role);

		boolean isNew = role.isNew();

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, role, merge);

			role.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		EntityCacheUtil.putResult(roleModelImpl.ENTITY_CACHE_ENABLED,
			roleImpl.class, role.getPrimaryKey(), role);

		return role;
	}

	protected role toUnwrappedModel(role role) {
		if (role instanceof roleImpl) {
			return role;
		}

		roleImpl roleImpl = new roleImpl();

		roleImpl.setNew(role.isNew());
		roleImpl.setPrimaryKey(role.getPrimaryKey());

		roleImpl.setMaRole(role.getMaRole());
		roleImpl.setTenRole(role.getTenRole());
		roleImpl.setGhiChu(role.getGhiChu());

		return roleImpl;
	}

	/**
	 * Returns the role with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the role
	 * @return the role
	 * @throws com.liferay.portal.NoSuchModelException if a role with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public role findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey((String)primaryKey);
	}

	/**
	 * Returns the role with the primary key or throws a {@link com.portlet.NoSuchroleException} if it could not be found.
	 *
	 * @param maRole the primary key of the role
	 * @return the role
	 * @throws com.portlet.NoSuchroleException if a role with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public role findByPrimaryKey(String maRole)
		throws NoSuchroleException, SystemException {
		role role = fetchByPrimaryKey(maRole);

		if (role == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + maRole);
			}

			throw new NoSuchroleException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				maRole);
		}

		return role;
	}

	/**
	 * Returns the role with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the role
	 * @return the role, or <code>null</code> if a role with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public role fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey((String)primaryKey);
	}

	/**
	 * Returns the role with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param maRole the primary key of the role
	 * @return the role, or <code>null</code> if a role with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public role fetchByPrimaryKey(String maRole) throws SystemException {
		role role = (role)EntityCacheUtil.getResult(roleModelImpl.ENTITY_CACHE_ENABLED,
				roleImpl.class, maRole);

		if (role == _nullrole) {
			return null;
		}

		if (role == null) {
			Session session = null;

			boolean hasException = false;

			try {
				session = openSession();

				role = (role)session.get(roleImpl.class, maRole);
			}
			catch (Exception e) {
				hasException = true;

				throw processException(e);
			}
			finally {
				if (role != null) {
					cacheResult(role);
				}
				else if (!hasException) {
					EntityCacheUtil.putResult(roleModelImpl.ENTITY_CACHE_ENABLED,
						roleImpl.class, maRole, _nullrole);
				}

				closeSession(session);
			}
		}

		return role;
	}

	/**
	 * Returns all the roles.
	 *
	 * @return the roles
	 * @throws SystemException if a system exception occurred
	 */
	public List<role> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the roles.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of roles
	 * @param end the upper bound of the range of roles (not inclusive)
	 * @return the range of roles
	 * @throws SystemException if a system exception occurred
	 */
	public List<role> findAll(int start, int end) throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the roles.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of roles
	 * @param end the upper bound of the range of roles (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of roles
	 * @throws SystemException if a system exception occurred
	 */
	public List<role> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = new Object[] { start, end, orderByComparator };

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<role> list = (List<role>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_ROLE);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_ROLE;
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<role>)QueryUtil.list(q, getDialect(), start,
							end, false);

					Collections.sort(list);
				}
				else {
					list = (List<role>)QueryUtil.list(q, getDialect(), start,
							end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the roles from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (role role : findAll()) {
			remove(role);
		}
	}

	/**
	 * Returns the number of roles.
	 *
	 * @return the number of roles
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_ROLE);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns all the nhanviens associated with the role.
	 *
	 * @param pk the primary key of the role
	 * @return the nhanviens associated with the role
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.portlet.model.nhanvien> getnhanviens(String pk)
		throws SystemException {
		return getnhanviens(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
	}

	/**
	 * Returns a range of all the nhanviens associated with the role.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the role
	 * @param start the lower bound of the range of roles
	 * @param end the upper bound of the range of roles (not inclusive)
	 * @return the range of nhanviens associated with the role
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.portlet.model.nhanvien> getnhanviens(String pk, int start,
		int end) throws SystemException {
		return getnhanviens(pk, start, end, null);
	}

	public static final FinderPath FINDER_PATH_GET_NHANVIENS = new FinderPath(com.portlet.model.impl.nhanvienModelImpl.ENTITY_CACHE_ENABLED,
			com.portlet.model.impl.nhanvienModelImpl.FINDER_CACHE_ENABLED,
			com.portlet.model.impl.nhanvienImpl.class,
			com.portlet.service.persistence.nhanvienPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getnhanviens",
			new String[] {
				String.class.getName(), "java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});

	static {
		FINDER_PATH_GET_NHANVIENS.setCacheKeyGeneratorCacheName(null);
	}

	/**
	 * Returns an ordered range of all the nhanviens associated with the role.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the role
	 * @param start the lower bound of the range of roles
	 * @param end the upper bound of the range of roles (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of nhanviens associated with the role
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.portlet.model.nhanvien> getnhanviens(String pk, int start,
		int end, OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };

		List<com.portlet.model.nhanvien> list = (List<com.portlet.model.nhanvien>)FinderCacheUtil.getResult(FINDER_PATH_GET_NHANVIENS,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				String sql = null;

				if (orderByComparator != null) {
					sql = _SQL_GETNHANVIENS.concat(ORDER_BY_CLAUSE)
										   .concat(orderByComparator.getOrderBy());
				}
				else {
					sql = _SQL_GETNHANVIENS;
				}

				SQLQuery q = session.createSQLQuery(sql);

				q.addEntity("dtbase_nhanvien",
					com.portlet.model.impl.nhanvienImpl.class);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				list = (List<com.portlet.model.nhanvien>)QueryUtil.list(q,
						getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_GET_NHANVIENS,
						finderArgs);
				}
				else {
					nhanvienPersistence.cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_GET_NHANVIENS,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	public static final FinderPath FINDER_PATH_GET_NHANVIENS_SIZE = new FinderPath(com.portlet.model.impl.nhanvienModelImpl.ENTITY_CACHE_ENABLED,
			com.portlet.model.impl.nhanvienModelImpl.FINDER_CACHE_ENABLED,
			com.portlet.model.impl.nhanvienImpl.class,
			com.portlet.service.persistence.nhanvienPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getnhanviensSize", new String[] { String.class.getName() });

	static {
		FINDER_PATH_GET_NHANVIENS_SIZE.setCacheKeyGeneratorCacheName(null);
	}

	/**
	 * Returns the number of nhanviens associated with the role.
	 *
	 * @param pk the primary key of the role
	 * @return the number of nhanviens associated with the role
	 * @throws SystemException if a system exception occurred
	 */
	public int getnhanviensSize(String pk) throws SystemException {
		Object[] finderArgs = new Object[] { pk };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_NHANVIENS_SIZE,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				SQLQuery q = session.createSQLQuery(_SQL_GETNHANVIENSSIZE);

				q.addScalar(COUNT_COLUMN_NAME,
					com.liferay.portal.kernel.dao.orm.Type.LONG);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_GET_NHANVIENS_SIZE,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	public static final FinderPath FINDER_PATH_CONTAINS_NHANVIEN = new FinderPath(com.portlet.model.impl.nhanvienModelImpl.ENTITY_CACHE_ENABLED,
			com.portlet.model.impl.nhanvienModelImpl.FINDER_CACHE_ENABLED,
			com.portlet.model.impl.nhanvienImpl.class,
			com.portlet.service.persistence.nhanvienPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"containsnhanvien",
			new String[] { String.class.getName(), String.class.getName() });

	/**
	 * Returns <code>true</code> if the nhanvien is associated with the role.
	 *
	 * @param pk the primary key of the role
	 * @param nhanvienPK the primary key of the nhanvien
	 * @return <code>true</code> if the nhanvien is associated with the role; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsnhanvien(String pk, String nhanvienPK)
		throws SystemException {
		Object[] finderArgs = new Object[] { pk, nhanvienPK };

		Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_NHANVIEN,
				finderArgs, this);

		if (value == null) {
			try {
				value = Boolean.valueOf(containsnhanvien.contains(pk, nhanvienPK));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (value == null) {
					value = Boolean.FALSE;
				}

				FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_NHANVIEN,
					finderArgs, value);
			}
		}

		return value.booleanValue();
	}

	/**
	 * Returns <code>true</code> if the role has any nhanviens associated with it.
	 *
	 * @param pk the primary key of the role to check for associations with nhanviens
	 * @return <code>true</code> if the role has any nhanviens associated with it; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsnhanviens(String pk) throws SystemException {
		if (getnhanviensSize(pk) > 0) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * Initializes the role persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.portlet.model.role")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<role>> listenersList = new ArrayList<ModelListener<role>>();

				for (String listenerClassName : listenerClassNames) {
					Class<?> clazz = getClass();

					listenersList.add((ModelListener<role>)InstanceFactory.newInstance(
							clazz.getClassLoader(), listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}

		containsnhanvien = new Containsnhanvien();
	}

	public void destroy() {
		EntityCacheUtil.removeCache(roleImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@BeanReference(type = bophanPersistence.class)
	protected bophanPersistence bophanPersistence;
	@BeanReference(type = nhanvienPersistence.class)
	protected nhanvienPersistence nhanvienPersistence;
	@BeanReference(type = rolePersistence.class)
	protected rolePersistence rolePersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	protected Containsnhanvien containsnhanvien;

	protected class Containsnhanvien {
		protected Containsnhanvien() {
			_mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
					_SQL_CONTAINSNHANVIEN,
					new int[] { java.sql.Types.VARCHAR, java.sql.Types.VARCHAR },
					RowMapper.COUNT);
		}

		protected boolean contains(String maRole, String maNhanVien) {
			List<Integer> results = _mappingSqlQuery.execute(new Object[] {
						maRole, maNhanVien
					});

			if (results.size() > 0) {
				Integer count = results.get(0);

				if (count.intValue() > 0) {
					return true;
				}
			}

			return false;
		}

		private MappingSqlQuery<Integer> _mappingSqlQuery;
	}

	private static final String _SQL_SELECT_ROLE = "SELECT role FROM role role";
	private static final String _SQL_COUNT_ROLE = "SELECT COUNT(role) FROM role role";
	private static final String _SQL_GETNHANVIENS = "SELECT {dtbase_nhanvien.*} FROM dtbase_nhanvien INNER JOIN dtbase_role ON (dtbase_role.maRole = dtbase_nhanvien.maRole) WHERE (dtbase_role.maRole = ?)";
	private static final String _SQL_GETNHANVIENSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM dtbase_nhanvien WHERE maRole = ?";
	private static final String _SQL_CONTAINSNHANVIEN = "SELECT COUNT(*) AS COUNT_VALUE FROM dtbase_nhanvien WHERE maRole = ? AND maNhanVien = ?";
	private static final String _ORDER_BY_ENTITY_ALIAS = "role.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No role exists with the primary key ";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(rolePersistenceImpl.class);
	private static role _nullrole = new roleImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<role> toCacheModel() {
				return _nullroleCacheModel;
			}
		};

	private static CacheModel<role> _nullroleCacheModel = new CacheModel<role>() {
			public role toEntityModel() {
				return _nullrole;
			}
		};
}